package com.towa.ninemanmorris;

import java.util.ArrayList;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class GroupActivity extends Activity {
	
	private Button joinGroupButton;
	private EditText groupTextField;
	private ListView groupList;
	
	private ArrayList<String> groupArray;
	
	private GroupTasker tasker;
	private String deviceId;
	
	private String player;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.group);

		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
		deviceId = prefs.getString("deviceId", "n/a");
		player = getIntent().getExtras().getString("player");
		
		joinGroupButton = (Button) findViewById(R.id.joinGroupButton);
		groupTextField = (EditText) findViewById(R.id.groupTextField);
		groupList = (ListView) findViewById(R.id.groupList);
		
		groupArray = getGroupList();
		
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, groupArray);
		groupList.setAdapter(adapter);
		
		joinGroupButton.setOnClickListener(new JoinOnClickListener());
	}
	
	private ArrayList<String> getGroupList() {
		tasker = new GroupTasker();
		return tasker.doInBackground("getGroups::" + player);
	}
	
	class JoinOnClickListener implements OnClickListener {

		public void onClick(View arg0) {
			String group = groupTextField.getText().toString();
			if (!group.equals("") && group != null) {
				new Tasker().doInBackground("groupAdd::" + player + "::" + group);
			}
		}
		
	}
	

}
